<?php
defined('BY_JYA') or exit('error');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	//搜索
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if($_RQ['mid']){
        $params['mid like']='%'.trim($_RQ['mid']).'%';
        $return['params']['mid']=$_RQ['mid'];
    }
    if($_RQ['status']){
        $params['status']=trim($_RQ['status']);
        $return['params']['status']=$_RQ['status'];
    }
	$tip='每个挂卖都是独立设置运营机制！独立的兑换规则！';
	$pindex = max(1, intval($_RQ['page']));
	if($_RQ['num']){
		$psize = $_RQ['num'];
	}else{
		$psize = 15;
	}
	$total='';
	$parameter=array(
		'params'=>$params,
		'fields'=>$fields,
		'limit'=>array($pindex,$psize),
	);
	$list=pdo_getslice('jy_guashop_goods',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
            $item['member']=cfc('core')->member_show($item['mid']);
			$log=pdo_get('jy_creditshop_log',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['logid']));
			$item['goods']=pdo_get('jy_creditshop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$log['goodsid']),array('title','thumb'));
			if($this->setting['guafee_rate'] && $item['price']){
				$item['guaprice']=$this->setting['guafee_rate']*$item['price']/100;
			}
		}
	}
	$return['list']=$list?$list:array();
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'挂卖列表'),
		),
        'export'=>'export',
		'list'=>array('data'=>'list','tip'=>$tip,'del_one'=>true,'edit'=>false,'list'=>array(
			array('type'=>'string','title'=>'会员ID','name'=>'mid'),
            array('name'=>'member','title'=>'会员信息','type'=>'member'),
			array('name'=>'goods.thumb','title'=>'商品图片','type'=>'image'),
			array('name'=>'goods.title','title'=>'商品内容','type'=>'string'),
			array('name'=>'price','title'=>'挂卖价格','type'=>'string'),
			array('name'=>'guaprice','title'=>'挂卖收益','type'=>'string'),
			array('type'=>'radio','title'=>'状态','name'=>'status','options'=>'goods_status'),
            array('name'=>'createtime','title'=>'时间','type'=>'time'),
            array('name'=>'successtime','title'=>'完成时间','type'=>'time'),
		),'batch'=>array(
			array('type'=>'post','url'=>'jy_guashop/list/setstatus0/','title'=>'一键待启用'),
			array('type'=>'post','url'=>'jy_guashop/list/setstatus1/','title'=>'一键挂卖中'),
			array('type'=>'post','url'=>'jy_guashop/list/setstatus2/','title'=>'一键结束'),
			array('type'=>'post','url'=>'jy_guashop/list/setstatus3/','title'=>'一键更下架'),
            array('type'=>'post','url'=>'jy_guashop/list/huishou/','title'=>'一键回收'),
		),'action'=>array(
			array('type'=>'modal','op'=>'look','title'=>'查看详情'),
		)),
        'search'=>array(
            array('type'=>'radio','width'=>15,'title'=>'状态','name'=>'status','options'=>'goods_status'),
            array('type'=>'choose','width'=>15,'action'=>'core/member.get_member/','name'=>'mid','title'=>'选择会员'),
        ),
	);
	$return['goods_status']=$this->goods_status;
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='look'){
	if($_RQ['id']){
		$item=$this->get('goods',array('id'=>$_RQ['id'],'uniacid'=>$_SESSION['uniacid']));
        $item['goods']=pdo_get('jy_creditshop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['goodsid']));
	}else{
		$item=array();
	}
	$return['item']=$item;
	$return['status']=array('0'=>'关闭','1'=>'开启');
	$htmls=array(
		'type'=>'look',
		'title'=>'创建挂卖商品',
		'look'=>array(
					array('type'=>'string','title'=>'挂卖价格','name'=>'price'),
					array('type'=>'string','title'=>'挂卖标题','name'=>'title'),
					array('name'=>'goods.thumb','title'=>'挂卖图片','type'=>'image'),
					array('type'=>'radio','title'=>'状态','name'=>'status','options'=>'goods_status'),
					array('type'=>'richtext','title'=>'挂卖详情','name'=>'goods.goodsdetail'),
		)
	);
	$return['goods_status']=$this->goods_status;
	$return['goodstype']=$this->goodstype;
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='setstatus0'){
	$ids=trim($_RQ['ids'],';');
	if($ids){
		$ids=explode(';',$ids);
	}else{
		$ids=array();
	}
	if($_RQ['id']){
		$ids[]=$_RQ['id'];
	}
	if(count($ids)==0){
		exi('请先选择活动','error');
	}
	foreach($ids as $id){
		$id=intval($id);
		if($id){
			pdo_update('jy_guashop_goods',array('status'=>0),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('操作成功！');
}
if($_RQ['op']=='setstatus1'){
	$ids=trim($_RQ['ids'],';');
	if($ids){
		$ids=explode(';',$ids);
	}else{
		$ids=array();
	}
	if($_RQ['id']){
		$ids[]=$_RQ['id'];
	}
	if(count($ids)==0){
		exi('请先选择活动','error');
	}
	foreach($ids as $id){
		$id=intval($id);
		if($id){
			pdo_update('jy_guashop_goods',array('status'=>1),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('操作成功！');
}
if($_RQ['op']=='setstatus2'){
	$ids=trim($_RQ['ids'],';');
	if($ids){
		$ids=explode(';',$ids);
	}else{
		$ids=array();
	}
	if($_RQ['id']){
		$ids[]=$_RQ['id'];
	}
	if(count($ids)==0){
		exi('请先选择活动','error');
	}
	foreach($ids as $id){
		$id=intval($id);
		if($id){
			pdo_update('jy_guashop_goods',array('status'=>2),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('操作成功！');
}
if($_RQ['op']=='setstatus3'){
    $ids=trim($_RQ['ids'],';');
    if($ids){
        $ids=explode(';',$ids);
    }else{
        $ids=array();
    }
    if($_RQ['id']){
        $ids[]=$_RQ['id'];
    }
    if(count($ids)==0){
        exi('请先选择活动','error');
    }
    foreach($ids as $id){
        $id=intval($id);
        if($id){
            pdo_update('jy_guashop_goods',array('status'=>3),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
        }
    }
    exi('操作成功！');
}
if($_RQ['op'] == 'del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_guashop_goods',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_guashop_goods',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			}
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='huishou'){
    $ids=trim($_RQ['ids'],';');
    if($ids){
        $ids=explode(';',$ids);
    }else{
        $ids=array();
    }
    if($_RQ['id']){
        $ids[]=$_RQ['id'];
    }
    if(count($ids)==0){
        exi('请先选择商品','error');
    }
    if($this->setting['seller']){
        $adminid=$this->setting['seller'][0]['mid'];
    }
    if(!$adminid){
        exi('未设置超级管理员！','error');
    }
    foreach($ids as $id){
        $id=intval($id);
        if($id){
            $goodsid=$id;
            $team=pdo_get('jy_guashop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$goodsid));
            if($team['status']!=1){
                exi('商品不在挂卖中，不能回购！','error');
            }
            $member=model('core/member')->detail($adminid,array('credit1','credit2','credit3','credit4','credit6','mobile','avatar','nickname'));
            if($member['credit2']<$team['price']){
                exi('超级管理员余额不足！','error');
            }
            model('core/member')->member_credit($adminid,'credit2',$log['fee'],false,'jy_guashop','管理员回收消耗');
            $log=array(
                'goodsid'=>$goodsid,
                'fee'=>$team['price'],
                'mid'=>$adminid,
                'sendtype'=>0,
                'address'=>'',
            );
            $order_id=$this->model('model')->up_canlog($log);
            $log['id']=$order_id;
            $params=array(
                'paytype'=>'credit',
            );
            $this->model('model')->canlog_pay($log,$params);
        }
    }
    exi('操作成功！');
}
if($_RQ['op']=='export'){
    $params=array('uniacid'=>$_SESSION['uniacid']);
    if($_RQ['title']){
        $params['title like']='%'.trim($_RQ['title']).'%';
        $return['params']['title']=$_RQ['title'];
    }
    if($_RQ['mid']){
        $params['mid']=intval($_RQ['mid']);
        $return['params']['mid']=$_RQ['mid'];
    }
    $list=pdo_getall('jy_guashop_goods',$params);
    if($list){
        foreach($list as &$item){
            $item['member']=cfc('core')->member_show($item['mid']);
            $log=pdo_get('jy_creditshop_log',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['logid']));
            $item['goods']=pdo_get('jy_creditshop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$log['goodsid']),array('title','thumb'));
            if($this->setting['guafee_rate'] && $item['price']){
                $item['guaprice']=$this->setting['guafee_rate']*$item['price']/100;
            }
            $item['nickname']=$item['member']['nickname'];
            $item['mobile']=$item['member']['mobile'];
            $item['goodstitle']=$item['goods']['title'];
            $item['statustitle']=$this->goods_status[$item['status']];
        }
    }
    $header=array(
        'mid'=>'会员ID',
        'nickname'=>'会员昵称',
        'mobile'=>'会员电话',
        'goodstitle'=>'商品内容',
        'price'=>'挂卖价格',
        'guaprice'=>'挂卖收益',
        'statustitle'=>'状态',
    );
    cfc('tools')->export2excel($header,$list,'挂卖列表');
}
?>